﻿using System;
using System.Collections.Generic;
using personalplaner.business.common;
using personalplaner.common;
using personalplaner.dataprovider;
using System.Collections.ObjectModel;

namespace personalplaner.databasewizard.services
{
    public interface IOdbcSourceService
    {
        IEnumerable<string> GetOdbcSources();

        IList<string> GetDatabases(string odbcSource, string user, string password);

        bool IsLoginValid(string odbcSource, string user, string password);
    }

    public class OdbcSourceService : IOdbcSourceService
    {
        public IEnumerable<string> GetOdbcSources()
        {
            using (new WaitCursor())
            {
                try
                {
                    return DataServices.DatabaseService.GetOdbcList();
                }
                catch (Exception e)
                {
                    //TODO: show a messagebox here with the error
                    Console.Write(e);
                }

                return null;
            }
        }

        public IList<string> GetDatabases(string odbcSource, string user, string password)
        {
            using (new WaitCursor())
            {
                try
                {
                    var lst = DataServices.DatabaseService.GetDatabaseList(new LoginData
                    {
                        DSN = odbcSource,
                        Password = password,
                        UserName = user
                    });

                    return lst != null ? new ObservableCollection<string>(lst) : new ObservableCollection<string>();
                }
                catch (Exception e)
                {
                    //TODO: show a messagebox here with the error
                    Console.Write(e);
                }

                return null;
            }
            //string strSQL = "sp_helpdb";
            //string odbcStrConnect = string.Format("DSN={0};UID={1};PWD={2};Database=master", odbcSource, user, password);
            //OdbcConnection odbcConn = new OdbcConnection(odbcStrConnect);

            //DataSet ds = new DataSet();
            //OdbcDataAdapter cmd = new OdbcDataAdapter(strSQL, odbcConn);
            //try
            //{
            //    cmd.Fill(ds, "dbs");
            //    odbcConn.Close();
            //}
            //catch (OdbcException exp)
            //{
            //    Gencode.Common.Util.ErrorHandler.LoggError("SQLWizz", exp);
            //    return false;
            //}
            //finally
            //{
            //    comboDatabse.Items.Clear();
            //    comboDatabse.Text = string.Empty;

            //    DataView dv = new DataView(ds.Tables["dbs"]);
            //    for (int i = 0; i < dv.Count; i++)
            //    {
            //        // Keine System-Datenbanken laden
            //        if (dv[i].Row[0].ToString() != "master" && dv[i].Row[0].ToString() != "model" && dv[i].Row[0].ToString() != "msdb" && dv[i].Row[0].ToString() != "tempdb")
            //            if (TestConnection(listODBC.Items[listODBC.SelectedIndex].ToString(), txtUser.Text, txtPwd.Text, dv[i].Row[0].ToString()))
            //            {
            //                comboDatabse.Items.Add(dv[i].Row[0].ToString());
            //            }
            //    }
            //}
        }

        public bool IsLoginValid(string odbcSource, string user, string password)
        {
            using (new WaitCursor())
            {
                try
                {
                    return DataServices.DatabaseService.IsLoginValid(odbcSource, user, password);
                }
                catch (Exception e)
                {
                    //TODO: show a messagebox here with the error
                    Console.Write(e);
                }

                return false;
            }
        }
    }
}
